<template>
  <div>
    <div v-if="isfang!='放大'&&isShou=='人员'">
      <el-form :inline="true" :model="searchForm" class="form-inline">
        <el-form-item label="人员姓名">
          <el-input v-model="searchForm.ryName" placeholder="请输入人员姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="人员编号">
          <el-input v-model="searchForm.ryCode" placeholder="请输入人员编号" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getupdateBmFP(false)">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-if="tableColumn.length>0"
      :data="tableData"
      ref="multipleTable"
      border
      v-loading="loading"
      show-summary
      :summary-method="getSummaries"
      :max-height="isfang=='放大'?350:600"
      style="width: 100%; margin-top: 20px;">
      <el-table-column prop="" label="序号" min-width="60" :formatter="nShowIndex" align="center"></el-table-column>
      <el-table-column v-for="item in tableColumn" :label="item.label" :prop="item.prop" min-width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row[item.prop] }}</span>
        </template>
        <el-table-column v-if="item.children" v-for="it in item.children" :prop="it.prop" :label="it.label">
          <template slot-scope="scope">
            <span v-if="scope.row[it.prop]">{{ $formatNum(scope.row[it.prop]) }}</span>
            <span v-else>0</span>
          </template>
        </el-table-column>
      </el-table-column>
      <!-- <el-table-column prop="deptPnum" label="机构人数" width="100"align="center"></el-table-column>
      <el-table-column label="人均奖金" min-width="100"align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.deptPnum!=0">{{(scope.row.jjJe*1/scope.row.deptPnum*1).toFixed(4)}}</span>
              <span v-else>0</span>
          </template>
      </el-table-column>
      <el-table-column prop="jjJe" label="奖金总额(元)" min-width="100"align="center"></el-table-column>
      <el-table-column prop="ryCode" align="center">
          <template slot="header" slot-scope="scope">
             <span>安全生产奖</span>
             <el-tooltip placement="top" effect="light" width="1000">
                  <div slot="content">
                  <span>可分配余额：<span class="font22 blue">20000</span>元</span>
                  <span class="ml30">已分配金额：<span class="font22 blue">10000</span>元</span>
                  <div style="display:none"></div>
                  <span class="ml30">剩余可分配额：<span class="font22 blue">10000</span>元</span>
                  </div>
                  <i class="el-icon-warning"></i>
              </el-tooltip>
              <el-button type="text">追加</el-button>
          </template>
          <el-table-column prop="ryCode" label="金额(元)" width="100"align="center"></el-table-column>
          <el-table-column prop="ryCode" label="备注" width="100"align="center"></el-table-column>
      </el-table-column> -->
    </el-table>
    <el-button v-if="isfang=='放大'" type="info" style="margin-top:20px;" @click="fangda"
               plain>全屏</el-button>
    <span v-if="tichuConfig && tichuConfig.id &&isShou=='人员'">关联已发奖金统计来源：</span>
    <el-select v-model="useSourceType"
               placeholder="不关联"
                v-if="tichuConfig && tichuConfig.id &&isShou=='人员'"
               @change="useSourceTypeChangeHandle"
               :disabled="tichuConfig.lockFlag!=0">
      <el-option label="不关联" value="0"></el-option>
      <el-option label="ECC统计奖金" value="1"></el-option>
      <el-option label="绩效统计奖金" value="2"></el-option>
    </el-select>

    <el-button style="margin-left: 10px;" v-if="tichuConfig && tichuConfig.id && tichuConfig.lockFlag==0&&isShou=='人员'"
               @click="tichuConfigUpdateHandle">
      保存
    </el-button>
    <div v-if="jiangjinFpJjfpdjb.jjLx == 1 && (!tichuConfig || !tichuConfig.id) &&isShou=='人员'" style="color: red">
      系统中还没有此年份的已发奖金统计，无法进行已发奖金关联扣除操作！！！
    </div>
  </div>
</template>

<script>
import {updateGrFP, updateTjFromTichu,getRylistByimport} from "@/api/jiangjin/jiangjinjjxxxb"
import tichuConfigController from "@/api/jiangjin/tichuConfigController";
import tichuDetailController from "@/api/jiangjin/tichuDetailController";
import formatUtil from "@/api/util/formatUtil";

export default {
  data() {
    return {
      innerVisible: true,
      tableData: [{}],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      tableColumn: [],
      loading: false,
      isfang: '',
      jiangjinFpJjfpdjb: {},
      tichuConfig: {},
      useSourceType: undefined,
      fpId: undefined,
      cfLx: undefined,
      searchForm:{},
      isShou:""
    }
  },
  methods: {
    init(fpId, cfLx, isfang,isShou,updateFlag) {
      this.fpId = fpId
      this.cfLx = cfLx
      this.isShou=isShou
      this.getupdateBmFP(updateFlag)
      this.isfang = isfang
    },
    getupdateBmFP(updateFlag) {
      this.loading = true
      updateGrFP(this.fpId, this.cfLx,this.searchForm.ryName||undefined,this.searchForm.ryCode||undefined,updateFlag).then(res => {
        if(res.data.data){
          this.tableData = res.data.data.tableData;
          this.tableColumn = res.data.data.tableColumn;
          var list=[]
          this.tableData.forEach(item=>{
            list.push(item['ryJj'])
          })
          var renyuanChild={
            list:list,
            tableData:this.tableData,
            tableColumn:this.tableColumn
          }
          this.$emit('renyuanChild',renyuanChild)
        }
        if(res.data.code==0){

          if(res.data.msg){
            
              this.$message.closeAll()
              this.$message.warning(res.data.msg)
          }else{
            this.$message.closeAll()
            this.$message.success('已更新数据')
          }
        }
        if(res.data.data.message){
          this.$message.closeAll()
          this.$message.warning(res.data.data.message)
        }
        
        this.jiangjinFpJjfpdjb = res.data.data.jiangjinFpJjfpdjb
        if (this.jiangjinFpJjfpdjb.jjLx == 1) {//只有年度奖金需要关联已发奖金统计
          this.tichuConfig = res.data.data.tichuConfig
          this.useSourceType = this.tichuConfig.useSourceType
          /*if (this.tichuConfig.lockFlag == 0 && this.useSourceType != 0) {
            this.useSourceTypeChangeHandle()
          }*/
        }
                  
        
        
        this.loading = false
      })
    },
    daochu(fpId, cfLx, isfang){
      this.cfLx = cfLx
        this.isfang = isfang
      getRylistByimport(fpId).then(res=>{
        this.tableData = res.data.data.tableData;
        this.tableColumn = res.data.data.tableColumn;
        var renyuanChild={
          tableData:this.tableData,
          tableColumn:this.tableColumn
        }
        this.$emit('renyuanChild',renyuanChild)
      })
    },
    //合计
    getSummaries(param){
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => {
          if(column.property!='ryCode'){
            return  Number(item[column.property])
          }
        });
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0).toFixed(2);
          sums[index] += ' ';
        } else {
          sums[index] = ' ';
        }
      });
      return sums;
    },
    //全屏
    fangda() {
      this.$emit('renyuanChild','缩小')
    },
    nShowIndex(row, column, cellValue, index) {
      return index + 1 + this.pageSize * (this.pageIndex - 1);
    },
    tichuConfigUpdateHandle() {
      this.$confirm("一个年度只允许关联一笔年奖。选定当前年份使用此种数据来源，并设定此笔年奖关联扣除奖金发放结果吗？").then(
        _ => {
          /*updateTjFromTichu(this.fpId, this.useSourceType).then(_ => {

          })*/
          this.tichuConfig.useSourceType = this.useSourceType
          if (this.useSourceType == 0) {
            this.tichuConfig.fpId = 0
          } else {
            this.tichuConfig.fpId = this.fpId
          }
          tichuConfigController.putObj(this.tichuConfig).then(res => {
            console.log("tichuConfig 保存完成")
            this.getupdateBmFP()
          })
        }
      )
    },
    /**
     * 在前台实现模拟计算，并不保存，在用户点击保存后，在后台重新计算，如果以后实得的计算公式发生变化，此处前台和后台都要修改
     */
    useSourceTypeChangeHandle() {
      /*if (this.tichuConfig.useSourceType == 0 || this.tichuConfig.fpId != this.fpId) {
        this.getupdateBmFP()
        return
      }*/
      //this.useSourceType = this.tichuConfig.useSourceType
      let _this = this
      /*if (this.useSourceType != this.tichuConfig.useSourceType) {

      }*/
      tichuDetailController.sumForResult({
        "TichuDetail_EQS_years": _this.jiangjinFpJjfpdjb.jjDateq.substring(0, 4),
        "TichuDetail_EQS_sourceType": _this.useSourceType,
        size: 9999
      }).then(({data}) => {
          let list = []
          if (data && data.data && data.data.records) {
            list = data.data.records
          }
          for (let i = 0; i < _this.tableData.length; i++) {
            this.$set(_this.tableData[i], 'jjJeTj', "0")
            let result = []
            if (list.length) {
              result = list.filter(item => item.ryCode === _this.tableData[i]["ryCode"])
            }
            let resultTotalFee = 0
            if (result.length > 0) {
              //console.log(_this.tableData[i])
              resultTotalFee = result[0]['resultTotalFee']
            }
            this.$set(_this.tableData[i], 'jjJeTj', resultTotalFee)
            //如果是【领导年终奖】，那么公式改成实得奖金=奖金总额-已发奖金+加扣奖；其他情况按照原来的公式走
            var findObj=this.tableColumn.find(item=>item.label=='领导年终奖');
            if(findObj){
              this.$set(_this.tableData[i], 'fpfee0', formatUtil.toDecimal(_this.tableData[i]['ryJj'] - resultTotalFee + _this.tableData[i]['jkfee0']*1, 2, false))
            }else{
              this.$set(_this.tableData[i], 'fpfee0', formatUtil.toDecimal(
                _this.tableData[i]['ydfee0']  + _this.tableData[i]['dxfee0'] + _this.tableData[i]['jkfee0']- (_this.tableData[i]['jjJeGs0'] ? _this.tableData[i]['jjJeGs0'] : 0) - resultTotalFee, 2, false))
            }
           }
        })
    }
  }
}
</script>

<style>
.ffdi {
  padding: 5px 10px;
}
</style>
